package com.css.dzj.dzjap.modules.todolist.constroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.todolist.entity.ApTodoEntity;
import com.css.dzj.dzjap.modules.todolist.service.ApTodoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 待办事项controller
 *
 * @author liyunyue
 */
@RequestMapping("/todo")
@RestController
public class ApTodoController {

    @Autowired
    private ApTodoService apTodoService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /**
     * 查询待办
     *
     * @param
     * @return
     * @author zhouchanghe
     * @description TODO
     * @date 2019/12/22 15:44
     **/

    @GetMapping("/findtodoPage")
    public R gettodoPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                         @RequestParam(name = "title", required = false) String title,
                         @RequestParam(name = "station", required = false) String station,
                         @RequestParam(name = "createPerson", required = false) String createPerson,
                         HttpServletRequest request

    ) {
        String s="";
        try{
            String status = Constant.STATUSDB;
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<ApTodoEntity> page = apTodoService.getPage(title, status, station, createPerson, pageRequest,request);
            HttpSession session= request.getSession();
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("代办事项", Constant.TODO,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("代办事项",Constant.TODO,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }

    }

    @GetMapping("/firstPageInfo")
    public R getFirstPageInfo(HttpServletRequest request){
        String s="";
        try{
            String todoStatus = Constant.STATUSDB;
            PageRequest todoRequest = PageRequest.of(0, 5);
            QueryResults<ApTodoEntity> todoInfo = apTodoService.getPage(null, todoStatus, null, null, todoRequest,request);

            String finishStatus = Constant.STATUSYB;
            PageRequest finishRequest = PageRequest.of(0, 5);
            QueryResults<ApTodoEntity> finishInfo = apTodoService.getPage(null, finishStatus, null, null, finishRequest,request);

            JSONObject data = new JSONObject();
            data.put("todo", todoInfo);
            data.put("finish", finishInfo);
            s = JSON.toJSONString(data);
            sysAuditService.saveSuccessAudit("待办事项", Constant.TODO,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", data);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("代办事项",Constant.TODO,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }

    }

    /**
     * 查询已办
     *
     * @param
     * @return
     * @author zhouchanghe
     * @description TODO
     * @date 2019/12/22 13:43
     **/

    @GetMapping("/findtoworkPage")
    public R gettoworkPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                           @RequestParam(name = "title", required = false) String title,
                           @RequestParam(name = "station", required = false) String station,
                           @RequestParam(name = "createPerson", required = false) String createPerson,
                           HttpServletRequest request
    ) {
        String s="";
        try {
            String status = Constant.STATUSYB;
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<ApTodoEntity> page = apTodoService.getPage(title, status, station, createPerson, pageRequest, request);
            HttpSession session= request.getSession();
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("代办事项", Constant.TODO,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("代办事项",Constant.TODO,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }
    }

    @PostMapping("/getMyTodo")
    public R getTodoList() {
        String s="";
        try {
            List<ApTodoEntity> apTodoEntityList = apTodoService.queryTodoByLocation();
            s = JSON.toJSONString(apTodoEntityList);
            sysAuditService.saveSuccessAudit("代办事项查询成功", Constant.TODO, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("apTodoEntityList", apTodoEntityList);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("代办事项查询失败",Constant.TODO,Constant.PT,Constant.SELECT,s,e,request);
            return  R.error("操作失败!");
        }
    }

    @PostMapping("/saveTodo")
    public R saveTodoList(@RequestBody ApTodoEntity apTodoEntity) {
        String s = "";
        try {
            //apTodoEntity.setLinkId("njbjhh");
            // apTodoEntity.setLocation("jjj");
            // apTodoEntity.setUrl("sss");
            apTodoService.saveDB(apTodoEntity);
            s = JSON.toJSONString(apTodoEntity);
            sysAuditService.saveSuccessAudit("待办事项保存成功", Constant.TODO, Constant.PT, Constant.ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("待办事项保存失败", Constant.TODO, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }

    }

}
